//
//  MallBuyTogetherListViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/11.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "MallBuyTogetherListViewController.h"
#import "MallBuyTogetherTableViewCell.h"
#import "SpecViewController.h"
#import "UIViewController+KNSemiModal.h"

@interface MallBuyTogetherListViewController ()<UITableViewDelegate, UITableViewDataSource, MallBuyTogetherCellDelegate>

@property (strong, nonatomic) UITableView *buyTogetherTableView;
@property (strong, nonatomic) NSMutableArray *buyTogetherInfoArr;
@property (nonatomic, assign) NSInteger page; // 网络请求页数

@end

@implementation MallBuyTogetherListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.page = 1;
	[self createUI];
	[self getBuyTogetherList];
	[self refreshData];
}

- (void)createUI {
	self.navigationItem.title = @"拼单列表";
	self.view.backgroundColor = kWhiteColor;
	_buyTogetherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
	_buyTogetherTableView.delegate = self;
	_buyTogetherTableView.dataSource = self;
	_buyTogetherTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.buyTogetherTableView registerNib:[UINib nibWithNibName:@"MallBuyTogetherTableViewCell" bundle:nil] forCellReuseIdentifier:@"buyTogetherCell"];
	[self.view addSubview:_buyTogetherTableView];
}

#pragma mark ==========更新数据==========
- (void)refreshData {
	__weak typeof (self) weakSelf = self;
	
	_buyTogetherTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
		NSLog(@"下拉刷新");
		weakSelf.page = 1;
		[weakSelf getBuyTogetherList];
	}];
	_buyTogetherTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
		NSLog(@"上拉加载");
		weakSelf.page++;
		[weakSelf getBuyTogetherList];
	}];
	
}

- (void)getBuyTogetherList
{
	NSDictionary *param = @{
							@"per_page":@"6", @"page":[NSString stringWithFormat:@"%ld", self.page]
							};
	NSString *url = [NSString stringWithFormat:@"%@/%ld/details-ecbuy", APIMalls, (long)self.goodId];
	
	[[NetworkTool sharedTool] requestWithName:@"获取拼单列表" URLString:url parameters:param method:@"GET" callBack:^(id responseObject) {
		
		NSArray *arr = responseObject[@"data"];
		if (self.page == 1) {
			[self.buyTogetherInfoArr removeAllObjects];
		}
		for (NSDictionary *dict in arr) {
			
			MallDetailBuyTogetherModel *model = [[MallDetailBuyTogetherModel alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.buyTogetherInfoArr addObject:model];
		}
		dispatch_async(dispatch_get_main_queue(), ^{
			[self.buyTogetherTableView reloadData];
			[self.buyTogetherTableView.mj_header endRefreshing];
			[self.buyTogetherTableView.mj_footer endRefreshing];
		});
	}];
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	MallBuyTogetherTableViewCell *cellBuyTogether = [tableView dequeueReusableCellWithIdentifier:@"buyTogetherCell"];
	cellBuyTogether.model = self.buyTogetherInfoArr[indexPath.row];
	cellBuyTogether.delegate = self;
	cellBuyTogether.selectionStyle = UITableViewCellSelectionStyleNone;
	return cellBuyTogether;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	return self.buyTogetherInfoArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	return 70;
	
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
}

- (void)joinTogether:(UIButton *)button :(NSString *)ecbuyId :(NSString *)hostBuyUserId
{
	NSString *myUserId = GetUserDefaule(USERINFO)[@"user_id"];
	if ([hostBuyUserId isEqualToString:myUserId]) {
		UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您不能加入自己开启的拼单" preferredStyle:UIAlertControllerStyleAlert];
		[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
		[self presentViewController:alert animated:YES completion:nil];
		return;
	}
	NSLog(@"点击加入拼单");
	SpecViewController *vc = [[SpecViewController alloc] initWithNibName:@"SpecViewController" bundle:nil];
	vc.model = self.model;
	vc.type = 3;
	vc.goodId = self.goodId;
	vc.delegate = self;
	vc.ecbuy_id = ecbuyId;
	[self presentSemiViewController:vc];
}

- (NSMutableArray *)buyTogetherInfoArr
{
	if (_buyTogetherInfoArr == nil) {
		_buyTogetherInfoArr = [[NSMutableArray alloc] init];
	}
	return _buyTogetherInfoArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
